package org.vectrics.common.util;



/**
 * Title:        Vectrics Server for Logging
 * Description:
 * Copyright:    Copyright (c) 2001
 * Company:      Vectrics
 * @author Mike Moore
 * @version 1.0
 */

import org.apache.log4j.*;
import java.util.*;
import junit.framework.TestCase;


public class PageListTestCase extends TestCase {
    private static Category log = Category.getInstance(PageListTestCase.class);

    public PageListTestCase(String name) {
        super(name);
    }


    public void tearDown()  {
        log.debug("tearDown() called");
    }
    

    public void setUp() throws java.text.ParseException
    {
        log.debug("setUp() finished");
    };

    public void testPaging() {
        log.info("testPaging() - making page list");
        TestPageList pageList = new TestPageList();

        log.info("testPaging() - page list set to display 10 items per page");
        pageList.setPageSize(10);

        log.info("testPaging() - getting first page");
        
        Vector items = pageList.getPage();

        log.info("testPaging() - doing tests");
        assertTrue(items != null);

        
        assertEquals("Page size should be 10", 10, items.size());
        assertEquals("User at should be user 0", "user_0", (String)items.elementAt(0));
        assertEquals("User at should be user 9", "user_9", (String)items.elementAt(9));
        String nextUserName = (String)pageList.getNextPageKey();
        assertEquals("Testing next page key", "user_10", nextUserName);
        String prevUserName = (String)pageList.getPreviousPageKey();
        assertEquals("Testing prev page key", null, prevUserName);

        items = pageList.getNextPage();
        assertEquals("Page size should be 10", 10, items.size());
        assertEquals("User at should be user 10", "user_10", (String)items.elementAt(0));
        assertEquals("User at should be user 19", "user_19", (String)items.elementAt(9));
        nextUserName = (String)pageList.getNextPageKey();
        assertEquals("Testing next page key for first 'next page'", "user_20", nextUserName);
        prevUserName = (String)pageList.getPreviousPageKey();
        assertEquals("Testing prev page key for first 'next page'", "user_0", prevUserName);
        

        items = pageList.getNextPage();
        assertEquals("Page size should be 10", 10, items.size());
        assertEquals("User at should be user 20", "user_20", (String)items.elementAt(0));
        assertEquals("User at should be user 29", "user_29", (String)items.elementAt(9));
        nextUserName = (String)pageList.getNextPageKey();
        assertEquals("Testing next page key", "user_30", nextUserName);
        prevUserName = (String)pageList.getPreviousPageKey();
        assertEquals("Testing prev page key", "user_10", prevUserName);


        items = pageList.getPreviousPage();
        assertEquals("Page size should be 10", 10, items.size());
        assertEquals("One prev - User at should be user 10", "user_10", (String)items.elementAt(0));
        assertEquals("One prev - User at should be user 19", "user_19", (String)items.elementAt(9));
        nextUserName = (String)pageList.getNextPageKey();
        assertEquals("One prev - Testing next page key for first 'prev page'", "user_20", nextUserName);
        prevUserName = (String)pageList.getPreviousPageKey();
        assertEquals("One prev - Testing prev page key for first 'prev page'", "user_0", prevUserName);

        // Prev pages past beginning should always land on first record.
        log.info("prev page 2");
        pageList.getPreviousPage();
        
        log.info("prev page 3");
        pageList.getPreviousPage();
        log.info("prev page 4");
        pageList.getPreviousPage();
        log.info("prev page 5");
        pageList.getPreviousPage();
        log.info("prev page 6");
        items = pageList.getPreviousPage();
        log.info("prev page 7 - got items");
        assertEquals("Many prev - User at should be user 0", "user_0", (String)items.elementAt(0));
        assertEquals("Many prev - User at should be user 9", "user_9", (String)items.elementAt(9));
        nextUserName = (String)pageList.getNextPageKey();
        assertEquals("Many prev - Testing next page key", "user_10", nextUserName);
        prevUserName = (String)pageList.getPreviousPageKey();
        assertEquals("Many prev - Testing prev page key", null, prevUserName);
        
        log.info("testPaging() - finished");
        
    }
   

}